package br.com.dev.tcc.view.sinistro;

import java.util.List;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import br.com.dev.tcc.R;
import br.com.dev.tcc.adapter.ListaSinistroAdapter;
import br.com.dev.tcc.bean.ResponseData;
import br.com.dev.tcc.bean.sinistro.Sinistro;
import br.com.dev.tcc.connection.WSClient;

import com.actionbarsherlock.app.SherlockFragment;

public class AcompanharSinistroFragment extends SherlockFragment {

	private boolean enableToSearch;
	private ProgressBar bar;
	private List<Sinistro> sinistros;
	private ListView listSinistros;
	private ListaSinistroAdapter adapter;
	private TextView tvSemSinistro;
	private boolean isSearching;
	private boolean isUpdating;
	private DelegateSinistroTask delegate;

	public AcompanharSinistroFragment(DelegateSinistroTask delegate) {
		this.delegate = delegate;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.sinistro_fragment_acomp, null);

		bar = (ProgressBar) view.findViewById(R.id.progressBar);
		listSinistros = (ListView) view.findViewById(R.id.list);
		tvSemSinistro = (TextView) view.findViewById(R.id.tvSemItens);
		tvSemSinistro.setText(R.string.nao_ha_sinistros);
		tvSemSinistro.setCompoundDrawables(null, null, null, null);

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		enableToSearch = true;
		buscaSinistros();
	}

	public void buscaSinistros() {
		if (enableToSearch)
			new SearchSinistrosAsync().execute();
	}

	/*
	 * Async que busca os sinistros cadastrados e os exibe na listview
	 */
	private class SearchSinistrosAsync extends
			AsyncTask<Void, Void, List<Sinistro>> {

		@Override
		protected void onPreExecute() {
			isSearching = true;
			bar.setVisibility(View.VISIBLE);
			listSinistros.setVisibility(View.GONE);
			tvSemSinistro.setVisibility(View.GONE);
		}

		@Override
		protected List<Sinistro> doInBackground(Void... params) {
			return delegate.busca();
		}

		@Override
		protected void onPostExecute(List<Sinistro> result) {

			sinistros = result;
			bar.setVisibility(View.GONE);
			isSearching = false;

			if (result != null && result.size() > 0) {
				adapter = new ListaSinistroAdapter(getActivity(), result);
				listSinistros.setVisibility(View.VISIBLE);
				listSinistros.setAdapter(adapter);
				listSinistros.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> a, View v,
							int position, long id) {
						delegate.onDelete(sinistros.get(position).getId());
					}
				});
			} else
				tvSemSinistro.setVisibility(View.VISIBLE);
		}
	}

	public void atualiza() {
		if (hasSinistro() && !isSearching && !isUpdating)
			new AsyncStatusSinistro().execute();
	}

	/*
	 * Classe que busca no servidor as atualizações de status dos sinistros
	 */
	private class AsyncStatusSinistro extends AsyncTask<Void, Sinistro, Void> {

		@Override
		protected void onPreExecute() {
			isUpdating = true;
		}

		@Override
		protected Void doInBackground(Void... params) {

			WSClient client = new WSClient();

			for (int i = 0; i < sinistros.size(); i++) {
				Sinistro sinistro = sinistros.get(i);
				ResponseData<String[]> result = client.buscarStatusSinistro(
						getActivity(), sinistro.getId());
				if (result.isSuccess()) {
					String status = result.getData()[0];
					String msg = result.getData()[1];
					sinistro.setStatus(status);
					sinistro.setStatusMensagem(msg);

					delegate.update(sinistro);

					publishProgress(sinistro);
				}
			}

			return null;
		}

		@Override
		protected void onProgressUpdate(Sinistro... values) {
			adapter.notifyDataSetChanged();
		}

		protected void onPostExecute(Void result) {
			isUpdating = false;
			delegate.onFinishUpdate();
		};
	}

	public boolean hasSinistro() {
		return sinistros != null && sinistros.size() > 0;
	}
	
	public interface DelegateSinistroTask {
		public List<Sinistro> busca();
		public void update(Sinistro sinistro);
		public void onFinishUpdate();
		public void onDelete(int idSinistro);
	}
}
